//
//  LXKHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 LXK. All rights reserved.
//

#import "LXKHomePageController.h"
#import "CK_MenuViewController.h"
#import "LXKNetWorkRequestManager.h"
#import "LXKCollectionViewCell.h"
#import "LXKCycleView.h"
#import "LXKHomeDetailController.h"
#import "LXKRadioController.h"

@interface LXKHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
//CollectionView
@property (nonatomic, strong) UICollectionView *collectionView;
//数组属性
@property (nonatomic, strong) NSMutableArray *dataSource;
//轮播图属性
@property (nonatomic, strong) LXKCycleView *cycleView;

@end

@implementation LXKHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cycleView = [[LXKCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = [@[@"  今日 · TODAY",@"  电台 · RADIO",@"  阅读 · READING",@"  话题 · TOPICS",@"  音乐 · MUSIC",@"  良品 · PRODUCTS"] mutableCopy];
    //初始化数据源
    self.dataSource = [NSMutableArray new];
    
    [LXKNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] Method:LXK_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dictData = jsonDict[@"data"];
            for (NSDictionary *itemDict in dictData[@"list"]) {
                LXKHomeModel *model = [LXKHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            self.cycleView.imgNameArray = [@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg"] mutableCopy];
        });
    }];
    //创建CollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    //设置layout
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 3)/3, (self.view.frame.size.width - 3)/3);
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 1.5;
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"LXKCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    // Do any additional setup after loading the view.
}

#pragma mark - UICollectionViewDelegaet
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //创建头轮播放图
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LXKHomeDetailController *detailVC = [[LXKHomeDetailController alloc]init];
    LXKHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LXKCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

//点击抽屉按钮
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
    
}

- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
      [self.navigationController popToRootViewControllerAnimated:NO];
    switch (index) {
        case 1:
        {
            LXKRadioController *radioVC = [[LXKRadioController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
